﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Interactivity;

namespace XWord.WPF.Behaviors
{
    public class StoryboardBehavior : Behavior<FrameworkElement>
    {

        private DispatcherTimer timer;

        private void ShowAnimation()
        {
            if (SuccessStoryboard == null || FailStoryboard == null) return;
            if (IsSuccess)
                AssociatedObject.BeginStoryboard(SuccessStoryboard);
            else
                AssociatedObject.BeginStoryboard(FailStoryboard);
            if (AutoRecover && RecoverStoryboard != null)
            {
                CreateTimer();
                timer.Start();
            }
        }

        private void CreateTimer()
        {
            if (timer != null) return;

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(RecoverSecond);
            timer.Tick += delegate
            {
                AssociatedObject.BeginStoryboard(RecoverStoryboard);
                timer.Stop();
            };
        }

        public bool IsSuccess
        {
            get { return (bool)GetValue(IsSuccessProperty); }
            set { SetValue(IsSuccessProperty, value); }
        }

        public static readonly DependencyProperty IsSuccessProperty =
            DependencyProperty.Register("IsSuccess", typeof(bool), typeof(StoryboardBehavior),
            new UIPropertyMetadata(false, null, new CoerceValueCallback((DependencyObject d, object baseValue) =>
            {
                (d as StoryboardBehavior).ShowAnimation();
                return baseValue;
            })));

        public Storyboard SuccessStoryboard
        {
            get { return (Storyboard)GetValue(SuccessStoryboardProperty); }
            set { SetValue(SuccessStoryboardProperty, value); }
        }

        public static readonly DependencyProperty SuccessStoryboardProperty =
            DependencyProperty.Register("SuccessStoryboard", typeof(Storyboard), typeof(StoryboardBehavior),
            new UIPropertyMetadata(null));

        public Storyboard FailStoryboard
        {
            get { return (Storyboard)GetValue(FailStoryboardProperty); }
            set { SetValue(FailStoryboardProperty, value); }
        }

        public static readonly DependencyProperty FailStoryboardProperty =
            DependencyProperty.Register("FailStoryboard", typeof(Storyboard), typeof(StoryboardBehavior),
            new UIPropertyMetadata(null));

        public bool AutoRecover
        {
            get { return (bool)GetValue(AutoRecoverProperty); }
            set { SetValue(AutoRecoverProperty, value); }
        }

        public static readonly DependencyProperty AutoRecoverProperty =
            DependencyProperty.Register("AutoRecover", typeof(bool), typeof(StoryboardBehavior),
            new UIPropertyMetadata(false));

        public double RecoverSecond
        {
            get { return (double)GetValue(RecoverSecondProperty); }
            set { SetValue(RecoverSecondProperty, value); }
        }

        public static readonly DependencyProperty RecoverSecondProperty =
            DependencyProperty.Register("RecoverSecond", typeof(double), typeof(StoryboardBehavior),
            new UIPropertyMetadata(3.0));

        public Storyboard RecoverStoryboard
        {
            get { return (Storyboard)GetValue(RecoverStoryboardProperty); }
            set { SetValue(RecoverStoryboardProperty, value); }
        }

        public static readonly DependencyProperty RecoverStoryboardProperty =
            DependencyProperty.Register("RecoverStoryboard", typeof(Storyboard), typeof(StoryboardBehavior),
            new UIPropertyMetadata(null));



    }
}
